import Room from "./Room";
import RoomTypes from "../Static/RoomTypes";

export default class PointRoom extends Room {

    //倍数
    multi = 0;

    en_class() {
        return 'PointRoom';
    }

    onCreate(){
        super.onCreate()
    }

    initRoom(gameMgr){
        super.initRoom(gameMgr)
    }

    onDestroy(){
        super.onDestroy()
        cc.find("Canvas").emit("game_room_destroy")
    }

    onRoomFinish(data){
        cc.gg.gameDataUtil.receiveFinish = true
        cc.gg.gameDataUtil.resultJustNow = false

        cc.gg.dataMgr.clearAllUserTotalScore(0)

        let id = data[0]
        let cards = data[1]
        let finishCard = data[2]
        // let groupTime = data[3]
        let groupTime = this.timer_cd

        finishCard = cc.gg.dataMgr.convToClientPokerValue(finishCard)

        let user = cc.gg.dataMgr.getUser(id)
        let resultObj = {
            id:id,
            name:user.name,
            cards:this.convToClientPokerValueArray(cards)
        }
        cc.find("Canvas").emit("flow_start_group_passive",id,resultObj,finishCard,groupTime)
    }

    onReport(scores){
        cc.logws("PointRoom.onReport:",scores)
        cc.gg.gameDataUtil.resultJustNow = false
        scores = scores[0]

        if(!cc.gg.gameDataUtil.receiveFinish){
            //point局分数不累计，清零
            cc.gg.dataMgr.clearAllUserTotalScore(0)
            let showScore = true   //等待阶段是否显示分数，或卢比
            cc.find("Canvas/UIManager").emit("ui_init_deal_info_board",null,null,showScore)
        }

        for(let i=0;i<scores.length;i++){
            let obj1 = scores[i]
            let id = obj1.id
            let score = obj1.score

            let userObj = cc.gg.dataMgr.getUser(id)
            if(userObj) {
                userObj.score = score
                cc.find("Canvas").emit("ui_update_head_score",id,userObj.score)
            }

            let fakeScore = userObj.score > 0 ? 0 : userObj.score
            cc.find("Canvas/UIManager").emit("ui_update_dealinfo_scores",obj1.id,fakeScore,0,false)
        }
        cc.find("Canvas/UIManager").emit("ui_show_score",false)
        // cc.gg.emitEvent("dealinfo_start_count_down",this.timer_cd)
        this.updateWinner(scores)
    }

    onResult(data){
        super.onResult(data)

        let isPractice = (this.mode & 0xf) === 0
        cc.logws("PointRoom收到result：",data)
        //在Point场中，将totalScore作为赢/输的钱
        let array = data[0]
        for(let i=0;i<array.length;i++){
            let netObj = array[i]
            let id = netObj.id
            let score = netObj.score
            let state = netObj.state
            let coin = netObj.coin

            let userObj = cc.gg.dataMgr.getUser(id)
            userObj.score = coin
            cc.log("结算：用户",id,"的最终结果为",coin)


            let totalCoin = isPractice ? 0 : coin

            let totalScoreStr = cc.gg.gameDataUtil.getStringOfRupee(totalCoin)
            cc.gg.gameDataUtil.setDealInfoTotalScore(id,totalScoreStr)
            cc.find("Canvas/UIManager").emit("ui_update_dealinfo_totalscore",id,totalScoreStr)
        }

        // cc.find("Canvas").emit("ui_update_dealinfo_totalscore")
    }
}